package org.honeyweb.gsoc.pages;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tapestry5.Block;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.PageLoaded;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.Service;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.corelib.components.Zone;
import org.apache.tapestry5.ioc.annotations.Inject;

import org.honeyweb.gsoc.model.Honeypots;
import org.honeyweb.gsoc.model.Organizations;
import org.honeyweb.gsoc.model.Users;
import org.honeyweb.gsoc.services.HoneypotsManager;
import org.honeyweb.gsoc.services.OrganizationsManager;
import org.honeyweb.gsoc.services.UsersManager;

@SuppressWarnings("unused")
public class Profile {
	
	private final Log logger = LogFactory.getLog(Profile.class);

	@Inject
	@Service("usersManager")
	private UsersManager usersManager;
	
	@Inject
	@Service("organizationsManager")
	private OrganizationsManager organizationsManager;
	
	@Inject
	@Service("honeypotsManager")
	private HoneypotsManager honeypotsManager;
	
	@Property
	@SessionState
	private Users user;	
	
	@Property
	@InjectComponent
	private Zone tabZone;
	
	@Component
	private Form form;
	
	@Inject
	private Block personalBlock;
	
	@Inject
	private Block personalEditBlock;
	
	@Inject
	private Block organizationBlock;
	
	@Inject
	private Block honeypotBlock;
	
	@Property
	private String login;
	
	@Property
	private String password;
	
	@Property
	private String retypePassword;
	
	@Property
	private String firstName;
	
	@Property
	private String lastName;
	
	@Property
	private String mail;
	
	private Organizations organization;
	
	@Property
	private Honeypots honeypotLoop;
	
	@Property
	private Organizations organizationLoop;
	
	@Persist
	@Property
	private int zone;

	
	public Organizations getOrganization() {
		logger.debug("User login value:"+user.getLogin());
		this.organization = organizationsManager.getOrganization(user);
		return organization;
	}
	
	public ArrayList<Honeypots> getHoneypots() {
		logger.debug("Organization name:"+getOrganization().getName());
		return honeypotsManager.getHoneypots(this.organization);
	}
	
	/*
	 * Check if the organization owns at least one honeypot
	 */
	public boolean getHoneypotTest(){
		return this.getOrganization().getHoneypotsId() != null;
	}
	
	
    Object onActionFromShowZone1()
    {
    	this.zone = 1;
		return tabZone.getBody();
    }
    
    Object onActionFromShowZone2()
    {
    	this.zone = 2;
		return tabZone.getBody();
    }
    
    Object onActionFromShowZone3()
    {
    	this.zone = 3;
		return tabZone.getBody();
    }
    
    Object onActionFromShowZone4()
    {
    	this.zone = 4;
		return tabZone.getBody();
    }
    
    public Object getActiveBlock()
    {
    	if( this.zone == 3 ){
    		return honeypotBlock;
    	}
    	if( this.zone == 2 ){
    		return organizationBlock;
    	}
    	if( this.zone == 4 ){
    		return personalEditBlock;
    	}
    	else{
    		return personalBlock;
    	}
    	
    }
    
	public ArrayList<Organizations> getListOrganizations() {
		return organizationsManager.getListOrganizations();	
	}
	
	String onSuccess(){
		usersManager.updateUser(login,firstName,lastName,mail,user.getIdUser());
		return "profile";
	};




	


}
